package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/spit")
@CrossOrigin
public class SpitController {
    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result findAll(){
        return new Result(true, StatusCode.OK, "查询成功", spitService.findAll());
    }

    @GetMapping("/{spitId}")
    public Result findById(@PathVariable String spitId){
        return new Result(true, StatusCode.OK, "查询成功", spitService.findById(spitId));
    }

    @PostMapping
    public Result save(@RequestBody Spit spit){
        spitService.save(spit);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping( "/{spitId}")
    public Result update(@PathVariable String spitId, @RequestBody Spit spit){
        spit.set_id(spitId);
        spitService.update(spit);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    @DeleteMapping("/{spitId}")
    public Result delete(@PathVariable String spitId){
        spitService.deleteById(spitId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("/comment/{parentid}/{page}/{size}")
    public Result comment(@PathVariable String parentid, @PathVariable int page, @PathVariable int size){
        Page<Spit> pageData = spitService.pageQuery(parentid, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Spit>(pageData.getTotalElements(), pageData.getContent()));
    }

    @PutMapping( "/thumbup/{spitId}")
    public Result addthumbup(@PathVariable String spitId){
        String userid = "11111";
        //先判断该用户是否已经点赞了。
        if(redisTemplate.opsForValue().get("spit_"+userid+"_"+spitId)!=null){
            return  new Result(true,StatusCode.REPERROR,"noooooooooo");
        }
        spitService.addthumbup(spitId);
        redisTemplate.opsForValue().set("spit_"+userid+"_"+spitId,"1");
        return new Result(true,StatusCode.OK,"ok");
    }

    /*
        * POST
        /spit/search/{page}/{size}
        标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
    ){
        Page<Spit> pageData = spitService.findByPage(map,page, size);
        PageResult pageResult=new PageResult(pageData.getTotalElements(),pageData.getContent());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
   /* *//*
    * POST
    /problem/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<Spit> list=spitService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
        
    }


}
